// ConcertController.java - 添加获取场地列表接口
package com.easy.concert.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.ConcertBean;
import com.easy.common.bean.SeatBean;
import com.easy.common.bean.VenueBean;
import com.easy.common.service.IConcertService;
import com.easy.common.service.IVenueService;
import com.easy.common.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("concert")
public class ConcertController {
    @Autowired
    IConcertService concertService;

    // 添加场地服务引用
    @DubboReference
    private IVenueService venueService;

    @GetMapping("/getConcertById/{id}")
    public Result getConcertById(@PathVariable("id") Integer id) {
        ConcertBean concert = concertService.getConcertWithVenue(id);
        if (concert == null) {
            return Result.fail(400,"未找到该演唱会");
        }
        return Result.success(concert);
    }
    //联合查询场地信息
    @GetMapping("/getVenueName")
    public Result getVenueName(Page<ConcertBean> page) {
        IPage<ConcertBean> getVenueName = concertService.getPageWithVenue(page);
        return Result.success(getVenueName);
    }


    @GetMapping("/getConcertByTitle/{title}")
    public Result getConcertByTitle(@PathVariable("title") String  title) {
        Object obj=concertService.getConcertByTitle(title);
        return Result.success(obj);
    }

    // 分页查询根据场地ID
    @GetMapping("/pageByVenue")
    public Result getPageByVenueId(
            @RequestParam(value = "venueId", required = false) Integer venueId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        try {
            Page<ConcertBean> page = new Page<>(current, size);
            IPage<ConcertBean> pageResult;

            if (venueId != null && venueId > 0) {
                pageResult = concertService.getPageByVenueId(venueId, page);
            } else {
                // 如果没有指定场地，返回所有演唱会
                pageResult = concertService.page(page, new LambdaQueryWrapper<ConcertBean>()
                        .eq(ConcertBean::getIsdel, 0)
                        .orderByDesc(ConcertBean::getStartdate));
            }

            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.success("分页查询失败");
        }
    }

    // 新增：获取所有场地接口
    @GetMapping("/venues")
    public Result getAllVenues() {
        try {
            List<VenueBean> venues = venueService.getAll(null);
            return Result.success(venues);
        } catch (Exception e) {
            return Result.success("获取场地列表失败");
        }
    }

    // 新增：联合查询接口（关键字+场地）
    @GetMapping("/pageByKeywordAndVenue")
    public Result getPageByKeywordAndVenue(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "venueId", required = false) Integer venueId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        try {
            Page<ConcertBean> page = new Page<>(current, size);
            IPage<ConcertBean> pageResult;

            LambdaQueryWrapper<ConcertBean> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ConcertBean::getIsdel, 0);

            // 添加场地筛选
            if (venueId != null && venueId > 0) {
                queryWrapper.eq(ConcertBean::getVenueid, venueId);
            }

            // 添加关键字搜索
            if (keyword != null && !keyword.trim().isEmpty()) {
                queryWrapper.and(wrapper -> wrapper
                        .like(ConcertBean::getTitle, keyword)
                        .or()
                        .like(ConcertBean::getArtist, keyword)
                );
            }

            queryWrapper.orderByDesc(ConcertBean::getStartdate);
            pageResult = concertService.page(page, queryWrapper);

            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.success("查询失败");
        }
    }

    @PostMapping("/add")
    public Result addConcert(@RequestBody ConcertBean concert) {
        int result = concertService.addConcert(concert);
        if(result >0){
            return Result.success(200,"添加成功", concert);
        }
        return Result.fail(400,"添加失败");
    }

    @PutMapping("/edit")
    public Result updateConcert(@RequestBody ConcertBean concertBean) {
        Object obj=concertService.updateConcert(concertBean);
        return Result.success(obj);
    }
    @DeleteMapping("/deleteConcert/{id}")
    public Result deleteConcert(@PathVariable("id") Integer id) {
        Object obj=concertService.deleteConcert(id);
        return Result.success(obj);
    }

    @PutMapping("/revertConcert/{id}")
    public Result revertConcert(@PathVariable("id") Integer id) {
        Object obj=concertService.revertConcert(id);
        return Result.success(obj);
    }
    @GetMapping("/all")
    public Result getAll(ConcertBean concertBean) {
        Object obj=concertService.getAll(concertBean);
        return Result.success(obj);
    }
//    @GetMapping("/page")
//    public Result getPage(ConcertBean concert, Page<ConcertBean> page ){
//        concertService.getPage(concert,page);
//        return Result.success(page);
//    }
@GetMapping("/page")
public Result getPage(ConcertBean concert, Page<ConcertBean> page) {
    IPage<ConcertBean> getVenueName = concertService.getPageWithVenue(page);
    return Result.success(getVenueName);
//        concertService.getPage(concert, page);
//        return Result.success(page);
    }


    @GetMapping("/multiCondition")
    public Result listByMultiCondition(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) Long startDate,
            @RequestParam(required = false) Long endDate,
            @RequestParam(required = false) Long current,
            @RequestParam(required = false) Long size) {
        // 默认分页参数
        if (current == null) current = 1L;
        if (size == null) size = 10L;

        Page<ConcertBean> page = concertService.listByMultiCondition(keyword, city, startDate, endDate, current, size);
        return Result.success(page);
    }

    Boolean startPrehead (Integer id){
        return true;
    }

    // 搜索演唱会
    @GetMapping("/search")
    public Result searchConcert(@RequestParam String keyword) {
        List<ConcertBean> concerts = concertService.searchConcert(keyword);
        return Result.success(concerts);
    }

    @GetMapping("/recommend")
    public Result getRecommendConcert() {
        List<ConcertBean> concerts = concertService.getRecommendConcert();
        return Result.success(concerts);
    }

}